<template>
  <div>
    <!--inline:代表的是行内表单 一行内可以放多个表单元素-->
    <el-form :inline="true" class="demo-form-inline" :model="cForm">
      <el-form-item label="一级分类">
        <el-select :disabled="!show" placeholder="请选择" v-model="cForm.category1Id" @change="getCategory2List">
          <el-option :label="c1.name" :value="c1.id" v-for="c1 in list1" :key="c1.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="二级分类">
        <el-select :disabled="!show" placeholder="请选择" v-model="cForm.category2Id" @change="getCategory3List">
          <el-option :label="c2.name" :value="c2.id" v-for="c2 in list2" :key="c2.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="三级分类" @click="getCategory3List">
        <el-select :disabled="!show" placeholder="请选择" v-model="cForm.category3Id" @change="handler3">
          <el-option :label="c3.name" :value="c3.id" v-for="c3 in list3" :key="c3.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'CategorySelect',
  //当正在添加属性或者修改属性时 不可操作分类
  props:['show'],
  data(){
    return{
     list1:[],//一级分类
     list2:[],//二级分类
     list3:[],//三级分类
      //收集分类ID
      cForm:{
       category1Id:'',
       category2Id:'',
       category3Id:'',
      }
    }
  },
  mounted() {
    //获取一级分类
    this.getCategory1List()
  },
  methods:{
    //一级分类
    async getCategory1List(){
      let result = await this.$API.attr.reqCategory1List()
      if (result.code === 200){
        this.list1 = result.data
      }
    },
    //二级分类
    async getCategory2List(){
      //清除数据
      this.list2 = []
      this.list3 = []
      this.cForm.category2Id = ''
      this.cForm.category3Id = ''
      let result = await this.$API.attr.reqCategory2List(this.cForm.category1Id)
      if (result.code === 200){
        this.list2 = result.data
      }
    },
    //三级分类
    async getCategory3List(){
      //清除数据
      this.list3 = []
      this.cForm.category3Id = ''
      let result = await this.$API.attr.reqCategory3List(this.cForm.category2Id)
      if (result.code === 200){
        this.list3 = result.data
      }
    },
    //获取商品
    handler3(){
      //将数据传给父组件
      this.$emit('getCategoryId',this.cForm)
    },
  }
}
</script>

<style scoped>

</style>
